package com.zenithmind.chat.service.impl;

import com.zenithmind.chat.pojo.entity.AiChat;
import com.zenithmind.chat.pojo.entity.AiChatMessage;
import com.zenithmind.chat.pojo.vo.AiChatVO;
import com.zenithmind.chat.service.AiChatCacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * AI对话缓存服务实现类
 * 遵循单一职责原则，专门负责缓存管理
 * 注意：这是一个简单的内存缓存实现，生产环境建议使用Redis
 */
@Slf4j
@Service
public class AiChatCacheServiceImpl implements AiChatCacheService {
    
    // 简单的内存缓存实现
    private final ConcurrentMap<String, AiChatVO> chatDetailCache = new ConcurrentHashMap<>();
    private final ConcurrentMap<String, List<AiChatMessage>> chatHistoryCache = new ConcurrentHashMap<>();
    private final ConcurrentMap<String, List<AiChatVO>> userChatListCache = new ConcurrentHashMap<>();
    
    @Override
    public void cacheChatDetail(String chatId, AiChatVO chatVO) {
        if (chatId != null && chatVO != null) {
            chatDetailCache.put(chatId, chatVO);
            log.debug("缓存对话详情: {}", chatId);
        }
    }
    
    @Override
    public AiChatVO getCachedChatDetail(String chatId) {
        if (chatId == null) {
            return null;
        }
        AiChatVO cached = chatDetailCache.get(chatId);
        if (cached != null) {
            log.debug("命中对话详情缓存: {}", chatId);
        }
        return cached;
    }
    
    @Override
    public void cacheChatHistory(String chatId, List<AiChatMessage> messages) {
        if (chatId != null && messages != null) {
            chatHistoryCache.put(chatId, messages);
            log.debug("缓存对话历史: {}, 消息数: {}", chatId, messages.size());
        }
    }
    
    @Override
    public List<AiChatMessage> getCachedChatHistory(String chatId) {
        if (chatId == null) {
            return null;
        }
        List<AiChatMessage> cached = chatHistoryCache.get(chatId);
        if (cached != null) {
            log.debug("命中对话历史缓存: {}, 消息数: {}", chatId, cached.size());
        }
        return cached;
    }
    
    @Override
    public void cacheUserChatList(String userId, List<AiChatVO> chats) {
        if (userId != null && chats != null) {
            userChatListCache.put(userId, chats);
            log.debug("缓存用户对话列表: {}, 对话数: {}", userId, chats.size());
        }
    }
    
    @Override
    public List<AiChatVO> getCachedUserChatList(String userId) {
        if (userId == null) {
            return null;
        }
        List<AiChatVO> cached = userChatListCache.get(userId);
        if (cached != null) {
            log.debug("命中用户对话列表缓存: {}, 对话数: {}", userId, cached.size());
        }
        return cached;
    }
    
    @Override
    public void evictChatCache(String chatId) {
        if (chatId != null) {
            chatDetailCache.remove(chatId);
            chatHistoryCache.remove(chatId);
            log.debug("清除对话缓存: {}", chatId);
        }
    }
    
    @Override
    public void evictUserCache(String userId) {
        if (userId != null) {
            userChatListCache.remove(userId);
            log.debug("清除用户缓存: {}", userId);
        }
    }
    
    @Override
    public void evictAllCache() {
        chatDetailCache.clear();
        chatHistoryCache.clear();
        userChatListCache.clear();
        log.info("清除所有缓存");
    }
}
